package com.study.thymeleaf.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.study.thymeleaf.core.R;
import com.study.thymeleaf.entity.Emp;
import com.study.thymeleaf.service.EmpService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmpController {

    @Resource
    EmpService empService;

    /**
     * 新增
     * @param emp
     * @return
     */
    @ApiOperation("新增")
    @PostMapping("/emp")
    public R<Object> save(@RequestBody Emp emp) {
        empService.save(emp);
        return R.ok(emp);
    }

    /**
     * 根据主键删除
     * @param empno
     * @return
     */
    @ApiOperation("根据主键删除")
    @DeleteMapping("/emp/{empno}")
    public R<Object> removeById(@PathVariable("empno") Integer empno) {
        empService.removeById(empno);
        return R.ok().setMsg("删除成功");
    }

    /**
     * 批量删除(物理删除)
     * @param empnos
     * @return
     */
    @ApiOperation("批量删除")
    @PostMapping("/removeBatch")
    public R<Object> removeBatch(@RequestBody List<Integer> empnos) {
        empService.removeBatchByIds(empnos);
        return R.ok().setMsg("删除成功");
    }

    /**
     * 根据主键+乐观锁版本号+逻辑删除列更新
     * @param emp
     * @return
     */
    @ApiOperation("根据主键+乐观锁版本号+逻辑删除列更新")
    @PutMapping("/emp")
    public R<Object> updateById(@RequestBody Emp emp) {
        empService.updateById(emp);
        return R.ok().setMsg("更新成功");
    }

    /**
     * 根据主键查找
     * @param empno 主键
     * @return
     */
    @ApiOperation("根据主键查找")
    @GetMapping("/emp/{empno}")
    public R<Emp> getById(@PathVariable("empno") Integer empno) {
        Emp emp = empService.getById(empno);
        return R.ok(emp);
    }

    @ApiOperation("分页查询")
    @PostMapping("/getEmpListByPage")
    public R<IPage<Emp>> selectList(@RequestBody Emp param) {
        return R.ok(empService.page(param.parse(), new QueryWrapper<>(param)));
    }

}
